package com.cms.dbUtils;


public class DaoBaseMethod {
	
	/**
	 * 生成基本的增删改查操作
	 * @param tableName
	 * @return
	 */
	public static StringBuffer createBaseMethod(String tableName,String idtypeString) {
		StringBuffer sb = new StringBuffer();
		sb.append("\r\n");
		StringBuffer findById = DaoBaseMethod.findById(tableName, idtypeString);
		sb.append(findById);
		StringBuffer deleteById = DaoBaseMethod.deleteById(tableName, idtypeString);
		sb.append(deleteById);
		StringBuffer save = DaoBaseMethod.save(tableName, idtypeString);
		sb.append(save);
		StringBuffer update = DaoBaseMethod.update(tableName, idtypeString);
		sb.append(update);
		return sb;
	}
	
	
	/**
	 * 生成基本的根据id查找
	 * @param tableName
	 * @return
	 */
	public static StringBuffer findById(String tableName,String idtypeString) {
		StringBuffer sb = new StringBuffer();
		sb.append("\r\n");
		if(null!=idtypeString&&idtypeString.equals("long")){
			
			sb.append("\tpublic " + StringToCap.initcap(tableName) + " findById(" + "Long id){\r\n")
			  .append("\t\treturn queryById(id,\"")
			  .append(tableName +"\"," + StringToCap.initcap(tableName)+".class);\r\n")
			  .append("\t}\r\n")
			  .append("\r\n");
		}else{
			sb.append("\tpublic " + StringToCap.initcap(tableName) + " findById(" + "Integer id){\r\n")
			  .append("\treturn queryById(id,\"")
			  .append(tableName +"\"," + StringToCap.initcap(tableName)+".class);\r\n")
			  .append("}\r\n");
		}
		
		return sb;
	}
	
	
	/**
	 * 生成根据id删除
	 * @param tableName
	 * @return
	 */
	public static StringBuffer deleteById(String tableName,String idtypeString) {
		StringBuffer sb = new StringBuffer();
		sb.append("\r\n");
		if(null!=idtypeString&&idtypeString.equals("long")){
			
			sb.append("\tpublic " + "boolean" + " deleteById(" + "Long id){\r\n")
			  .append("\t\treturn delete(\"delete from "+ tableName + " where id = ?\" , id);\r\n"  )
			  .append("\t}\r\n")
			  .append("\r\n");
		}else{
			sb.append("\tpublic " + "boolean" + " deleteById(" + "Integer id){\r\n")
			.append("\t\treturn delete(\"delete from "+ tableName + " where id = ?\" , id);\r\n"  )
			 .append("\t}\r\n")
			 .append("\r\n");
		}
		
		return sb;
	}
	
	
	/**
	 * 生成保存方法
	 * @param tableName
	 * @return
	 */
	public static StringBuffer save(String tableName,String idtypeString) {
		StringBuffer sb = new StringBuffer();
		sb.append("\tpublic " + "Long" + " save(" + StringToCap.initcap(tableName) + " " +StringToCap.initcapEent(tableName)+"){\r\n")
		  .append("\t\treturn save(" + StringToCap.initcapEent(tableName) + "," + " \"" + tableName + "\" " + ");\r\n"  )
		  .append("\t}\r\n")
		  .append("\r\n");
		
		return sb;
	}
	
	/**
	 * 生成保存方法
	 * @param tableName
	 * @return
	 */
	public static StringBuffer update(String tableName,String idtypeString) {
		StringBuffer sb = new StringBuffer();
		sb.append("\tpublic " + "boolean" + " update(" + StringToCap.initcap(tableName) + " " +StringToCap.initcapEent(tableName)+"){\r\n")
		  .append("\t\treturn update(" + StringToCap.initcapEent(tableName) + "," + " \"" + tableName + "\" " + ");\r\n"  )
		  .append("\t}\r\n")
		  .append("\r\n");
		
		return sb;
	}
	
	
	

}
